﻿<#
# 本脚本的意义 : 去maven化
#>
# --------------------------------------------------------Load Other Scripts---------------------------------------------------------------- 
. $PSScriptRoot\base.ps1 ;

$config = @{
                                            BDC_GXJH_BASE = @{
                                                PATH = $paths[0];
                                            };
<#
                                            BDC_GXJH_AREA =  @{
                                                PATH = $paths[1];
                                            };

                                            BDC_GXJH_NEW =  @{
                                                PATH = $paths[2];
                                            };
#>
                                           };

  <# 将本项目所依赖的maven引入的jar包全部放到项目根目录下的jars文件夹下#>
function  outputMavenDependencies ($mavenProjectPath)
{
    $targetFolderPath = [System.IO.Path]::Combine($mavenProjectPath,"jars");

    cleanFolder $targetFolderPath;
  
    outputMavenDependenciesTo $mavenProjectPath $targetFolderPath;
    
     # 将jars目录及其下所有的文件放到svn的管理之下
     svn_folder $targetFolderPath;

    return   $targetFolderPath ;
}

function svn_folder ($targetFolderPath)
{
      if(!(isVersionControlled $targetFolderPath))
        {
            call_svn_add_command $targetFolderPath ;
        }  

    #将生成的jar包添加到svn管理之下
     $jarPathCollection = Get-ChildItem $targetFolderPath | %{[System.IO.Path]::Combine($targetFolderPath , $_)};
     foreach ($jarPath in $jarPathCollection)
     {
        if( isVersionControlled $jarPath){continue;}
        call_svn_add_command $jarPath
     } 
}

function dealConfigFile ($mavenProjectPath)
{
    _dealConfigFile $mavenProjectPath;
    deal_classpath_file_appendReferencelibs $mavenProjectPath;
    removePOMFile $mavenProjectPath;
}

<# 处理eclipse的配置文件.project和.classpath #>
function _dealConfigFile ($mavenProjectPath)
{
   deal_project_file_removeMavenEnvironment $mavenProjectPath;
   deal_classpath_file_removeMavenEnvironment $mavenProjectPath;
}

function deal_project_file_removeMavenEnvironment ($mavenProjectPath)
{
    $currentPath = (pwd).Path;
    cd $mavenProjectPath

     $xmldata = [xml](Get-Content ".\.project")
    $a =  $xmldata.projectDescription.buildSpec.buildCommand | ?{$_.name -match "maven"}
    $a.ParentNode.RemoveChild($a);

    $c = $xmldata.SelectSingleNode("/projectDescription/natures/nature[contains(.,'maven')]");
      $c.ParentNode.RemoveChild($c);

    $absolutionPath = [System.IO.Path]::Combine($mavenProjectPath,".project");
    #$xmldata.Save( $absolutionPath); 
    saveXMLFileWithUFT8Encoding $absolutionPath $xmldata;

    cd $currentPath ;    
}

function deal_classpath_file_removeMavenEnvironment ($mavenProjectPath)
{
    $currentPath = (pwd).Path;
    cd $mavenProjectPath

     $xmldata = [xml](Get-Content ".\.classpath")
    $c = $xmldata.SelectSingleNode("/classpath/classpathentry[contains(@path,'MAVEN') and @kind='con']");
      $c.ParentNode.RemoveChild($c);

    $absolutionPath = [System.IO.Path]::Combine($mavenProjectPath,".classpath");
    #$xmldata.Save( $absolutionPath); 
    saveXMLFileWithUFT8Encoding $absolutionPath $xmldata;

    cd $currentPath ;    
}

function deal_classpath_file_appendReferencelibs($mavenProjectPath)
{
    $targetFolderPath = [System.IO.Path]::Combine($mavenProjectPath,"jars");
    $currentPath = (pwd).Path;
    cd $mavenProjectPath

        $xmldata = [xml](Get-Content ".\.classpath");
        foreach ($item in (Get-ChildItem $targetFolderPath))
        {
            $fileName = $item.Name;
            # 创建新的结点:
            $neweNode = $xmldata.CreateElement("classpathentry");
            $neweNode.SetAttribute("kind", "lib");
            $neweNode.SetAttribute("path",("./jars/{0}" -f $fileName));
            # 插入新结点:
            $xmldata.classpath.AppendChild($neweNode)
        }
    $absolutionPath = [System.IO.Path]::Combine($mavenProjectPath,".classpath");
    #$xmldata.Save( $absolutionPath); 
    saveXMLFileWithUFT8Encoding $absolutionPath $xmldata;

        cd $currentPath;    
}

function removePOMFile ($mavenProjectPath)
{
    $pomFilePath = [System.IO.Path]::Combine($mavenProjectPath,"pom.xml");
    if (Test-Path $pomFilePath)
    {
        Remove-Item   $pomFilePath;
    }
}

#--------------------------------------------------------Begin------------------------------------------------------------------------
<#输出配置文件设置的每个项目的依赖#>
function begin_outputMavenDependencies ()
{
    foreach ($key in $config.Keys)
    {
        $baseFolderPath= $config[$key].PATH;
        $basePathCollection = Get-ChildItem $baseFolderPath | %{[System.IO.Path]::Combine($baseFolderPath , $_)};
        foreach ($folderPath in $basePathCollection)
        {
           # 导出所有maven依赖;并将其进行svn的添加
           outputMavenDependencies $folderPath;
        }
    }    
}
<# 处理配置文件下每个项目的配置文件 #>
function begin_dealConfigFile()
{
    foreach ($key in $config.Keys)
    {
        $baseFolderPath= $config[$key].PATH;
        $basePathCollection = Get-ChildItem $baseFolderPath | %{[System.IO.Path]::Combine($baseFolderPath , $_)};
        foreach ($folderPath in $basePathCollection)
        {
           #处理配置文件
           dealConfigFile $folderPath;
        }
    }    
}

function beginDealOneFolder ($folderPath)
{
    # 导出所有maven依赖;并将其进行svn的添加
    outputMavenDependencies $folderPath;
    #处理配置文件
    #dealConfigFile $folderPath;    
}

# 针对配置文件中的所有项目
 begin_outputMavenDependencies
 begin_dealConfigFile

# 针对单一项目
$folderPath = "D:\Projects\BDC_GXJH_ALL\BDC_GXJH_AREA\BDC_GXJH_DABU";
#beginDealOneFolder $folderPath;